import express from 'express';
const app = express()

import cors from 'cors' //解决跨域问题

app.use(cors())
import bodyParser from 'body-parser'
import multiparty from 'connect-multiparty'

// 处理 x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ extended: true }))
// 处理 mutipart/form-data
app.use(multiparty())
// 处理 application/json
app.use(bodyParser.json())
 import conMysql from './mysql.js';
// 一个简单的测试接口
// app.get('/testa',(req,res)=>{
//     res.send('测试用的接口aaaaaaaaaaa')
// })
app.get('/getNews', (req, res) => {
	let sql = 'select * from news'
	conMysql(sql).then(result => {
		res.send(result)
	})
})
app.get('/TypeNews', (req, res) => {
    const { typeid } = req.query;
    let sql = `select * from news where typeid = ${typeid}`
    conMysql(sql)
        .then(result => {
            res.send(result);
        })
        .catch(err => {
            res.status(500).send({ message:'错误：' + err.message });
        });
});
app.get('/IdNews', (req, res) => {
    const { id } = req.query;
    let sql = `select * from news where newid = ${id}`
    conMysql(sql)
        .then(result => {
            res.send(result);
        })
        .catch(err => {
            res.status(500).send({ message:'错误：' + err.message });
        });
});
app.get('/SelectNews', (req, res) => {
    const { select } = req.query;
    let sql = `select * from news where title like '%${select}%'`
    conMysql(sql)
        .then(result => {
            res.send(result);
        })
        .catch(err => {
            res.status(500).send({ message:'错误：' + err.message });
        });
});
app.post('/addnews', (req, res) => {
    const { title, content,author,image,type,publishDate} = req.body;
    console.log(title, content,author,image,type,publishDate)
    let sql = `INSERT INTO news (title, content,author,imageName,typeid,publishdate) VALUES ("${title}", "${content}", "${author}", "${image}", ${type}, "${publishDate}")`;
    conMysql(sql)
        .then(result => {
            if (result.affectedRows > 0) {
                res.send({
                    status: 200,
                    message: '成功添加'
                });
            } else {
                res.send({
                    status: 202,
                    message: '添加失败'
                });
            }
        })
        .catch(err => {
            res.status(500).send({ message: '错误：' + err.message });
        });
});
app.get('/deleteNews', (req, res) => {
    const { id } = req.query;
    let sql = `DELETE FROM news WHERE newid = ${id}`;
    conMysql(sql)
        .then(result => {
            if (result.affectedRows > 0) {
                res.send({
                    status: 200,
                    message: '删除成功'
                });
            } else {
                res.send({
                    status: 202,
                    message: '删除失败'
                });
            }
        })
        .catch(err => {
            res.status(500).send({ message: '错误：' + err.message });
        });
});
app.post('/updateNews', (req, res) => {
    const {id,title,content ,imageName,author,publishdate,typeid} = req.body;
    let sql = `UPDATE news SET title = "${title}", content = "${content}", imageName = "${imageName}" , author = "${author}", publishdate = "${publishdate}", typeid = ${typeid} WHERE newid = ${id}`;
    conMysql(sql)
        .then(result => {
            if (result.changedRows > 0) {
                res.send({
                    status: 200,
                    message: '更新成功'
                });
            } else {
                res.send({
                    status: 202,
                    message: '更新失败'
                });
            }
        })
        .catch(err => {
            res.status(500).send({ message: '错误：' + err.message });
        });
});
//监听node服务器的端口号
app.listen(3001, () => {
	console.log('恭喜你，服务器启动成功')
})